//
//  JBWLoginViewController.m
//  HYSDK
//
//  Created by 崔志伟 on 2018/7/25.
//  Copyright © 2018年 崔志伟. All rights reserved.
//

#import "JBWLoginViewController.h"
#import "JBWForgetPassWordViewController.h"
#import "JBWRegisterWordViewController.h"
#import "JBWSimpleRegisterWordViewController.h"
#import "JBWMainViewController.h"
#import "JBWUserRecordViewController.h"
#import "JBWPOPRealNameAuthenticationViewController.h"

#import "JBWSDKInstance.h"
#import "JBWMoveMenuView.h"
#import "JBWUserRecordListView.h"
#define loginBgViewHight 280.0f

@interface JBWLoginViewController ()
@property (nonatomic,strong) UIView *editBgView  ;
@property (nonatomic,strong) UITextField  * phoneTextField ;
@property (nonatomic,strong) UITextField  * passwordTextField ;
@property (nonatomic,strong) UIButton * loginBtn  ;
@property (nonatomic,strong) UIView * registerView ;
@property (nonatomic,strong) JBWUserRecordListView *userRecordListView ;
@property (nonatomic,assign) BOOL  isShowUserRecord;


@end

@implementation JBWLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
     self.view.backgroundColor = [UIColor clearColor];
    [self setUpSubView];
}

- (void)setUpSubView {
    self.bgContentViewHight = loginBgViewHight;
    [self setUpEditBgView];
    [self setUpLoginBtn];
    [self setUpRegisterView];
}
- (void)removeAllSubView{
    [self removeBgContentViewAllSubView];
}
- (void)setUpEditBgView {
    
    //手机
    self.editBgView = [[UIView alloc]initWithFrame:CGRectMake(20, 75, MAIN_BG_VIEW_WIDTH-40, 89)];
    self.editBgView .backgroundColor = [UIColor whiteColor];
    self.editBgView .layer.cornerRadius = 5.f;
    self.editBgView .layer.masksToBounds = YES;
    [self.bgContentView addSubview:self.editBgView ];
    self.phoneTextField = [[UITextField alloc]initWithFrame:CGRectMake(5, 0, CGRectGetWidth(self.editBgView .frame)-40, 44)];
    self.phoneTextField.font =  [UIFont systemFontOfSize:13];
    self.phoneTextField.placeholder = @"手机号码";
    [self.editBgView  addSubview:self.phoneTextField];
    UIButton *recordBtn = [[UIButton alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.phoneTextField.frame), 0, 30, 44)];
    [recordBtn setImage:GetImageNamed(@"sdk_login_ic_down") forState:UIControlStateNormal];
    [recordBtn setImage:GetImageNamed(@"sdk_login_ic_up") forState:UIControlStateSelected];
    [recordBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [recordBtn addTarget:self action:@selector(recordBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.editBgView  addSubview:recordBtn];
    
    //密码
    UIView *centerLine = [[UIView alloc]initWithFrame:CGRectMake(0, 44, CGRectGetWidth(self.editBgView .frame), 1)];
    centerLine.backgroundColor = JBW_HEX_COLOR(0xF2F2F6);
    [self.editBgView  addSubview:centerLine];
    self.passwordTextField = [[UITextField alloc]initWithFrame:CGRectMake(5, 45, CGRectGetWidth(self.editBgView .frame)-70, 44)];
    self.passwordTextField.placeholder = @"密码";
    self.passwordTextField.secureTextEntry = YES;
    self.passwordTextField.font =  [UIFont systemFontOfSize:13];
    [self.editBgView  addSubview:self.passwordTextField];
    UIButton *forgetPasswordBtn = [[UIButton alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.passwordTextField.frame), 45, 60, 44)];
    [forgetPasswordBtn setTitle:@"忘记密码?" forState:UIControlStateNormal];
    forgetPasswordBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [forgetPasswordBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [forgetPasswordBtn addTarget:self action:@selector(forgetPasswordBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [self.editBgView  addSubview:forgetPasswordBtn];

}

-(void)setUpLoginBtn{
    
    self.loginBtn = [[UIButton alloc]initWithFrame:CGRectMake(20, CGRectGetMaxY(self.editBgView.frame)+15, MAIN_BG_VIEW_WIDTH-40, 44)];
    self.loginBtn.backgroundColor = [UIColor orangeColor];
    [self.loginBtn setTitle:@"进入游戏" forState:UIControlStateNormal];
    self.loginBtn .layer.cornerRadius = 5.f;
    self.loginBtn .layer.masksToBounds = YES;
    [self.loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.loginBtn addTarget:self action:@selector(loginBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [self.bgContentView addSubview:self.loginBtn];
}

- (void)setUpRegisterView{
    
    //一键注册
    self.registerView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.loginBtn.frame), MAIN_BG_VIEW_WIDTH, loginBgViewHight-CGRectGetMaxY(self.loginBtn.frame))];
    [self.bgContentView addSubview:self.registerView];
    
    UIButton * simpleRegisterBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 80, CGRectGetHeight(self.registerView.frame))];
    [simpleRegisterBtn setTitle:@"一键注册" forState:UIControlStateNormal];
    [simpleRegisterBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    simpleRegisterBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [simpleRegisterBtn addTarget:self action:@selector(simpleRegisterBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [self.registerView addSubview:simpleRegisterBtn];
    
    
    //手机号注册
    UIButton *registerBtn = [[UIButton alloc]initWithFrame:CGRectMake(MAIN_BG_VIEW_WIDTH-110, 0, 80, CGRectGetHeight(self.registerView.frame))];
    [registerBtn setTitle:@"手机号注册" forState:UIControlStateNormal];
    [registerBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    registerBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [registerBtn addTarget:self action:@selector(registerBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [self.registerView addSubview:registerBtn];
}



#pragma mark -- 用户记录
- (void)recordBtnAction:(UIButton *)sender{
    
    if (!self.isShowUserRecord) {
        self.userRecordListView = [[JBWUserRecordListView alloc]initWithFrame:CGRectMake(20, 115, MAIN_BG_VIEW_WIDTH-40, 89)];
        __block  __weak JBWLoginViewController *weakSelf = self;
        self.userRecordListView.userRecordListBlock = ^{
            JBWUserRecordViewController *userRecordVC = [JBWUserRecordViewController new] ;
            userRecordVC.dismissBlock = ^{
                [weakSelf setUpSubView];
            };
            [weakSelf removeAllSubView];
            [weakSelf presentViewController:userRecordVC animated:NO completion:nil];
        };
        [self.bgContentView addSubview:self.userRecordListView];
       
    }else{
        [self.userRecordListView removeFromSuperview];
    }
    self.isShowUserRecord =  !self.isShowUserRecord ;
    sender.selected =  self.isShowUserRecord;
    [self hideOrShowSomeView];
}

- (void)hideOrShowSomeView{
    self.registerView.hidden = self.isShowUserRecord;
    self.passwordTextField.hidden = self.isShowUserRecord;
    self.loginBtn.hidden = self.isShowUserRecord;
}


#pragma mark -- 忘记密码
- (void)forgetPasswordBtnAction{
    
    JBWForgetPassWordViewController *forgetPassWordVC = [JBWForgetPassWordViewController new] ;
    forgetPassWordVC.dismissBlock = ^{
        [self setUpSubView];
    };
    [self removeAllSubView];
    [self presentViewController:forgetPassWordVC animated:NO completion:nil];
}
#pragma mark -- 手机号注册
- (void)registerBtnAction{

    JBWRegisterWordViewController *registerWordVC = [JBWRegisterWordViewController new] ;
    registerWordVC.dismissBlock = ^{
        [self setUpSubView];
    };
    [self removeAllSubView];
    [self presentViewController:registerWordVC animated:NO completion:nil];
    
}
#pragma mark -- 一键注册
- (void)simpleRegisterBtnAction{
    
    JBWSimpleRegisterWordViewController *simpleRegisterWordVC = [JBWSimpleRegisterWordViewController new] ;
    simpleRegisterWordVC.dismissBlock = ^{
        [self setUpSubView];
    };
    [self removeAllSubView];
    [self presentViewController:simpleRegisterWordVC animated:NO completion:nil];
    
}
#pragma mark --登录
- (void)loginBtnAction{
    
    [self dismissViewControllerAnimated:NO completion:^{
        [self removeAllSubView];
      
      JBWMoveMenuView *moveMenuView= [[JBWMoveMenuView alloc]initWithFrame: CGRectMake(0, 100, 50, 50)];
        [JBW_KEY_WINDOW addSubview:moveMenuView];
        moveMenuView.parentView = JBW_KEY_WINDOW;
        __block __weak JBWMoveMenuView *weakMoveBth = moveMenuView;
        moveMenuView.selectItemBlcok = ^(NSInteger index) {
            JBWMainViewController *mainViewController = [JBWMainViewController new] ;
            mainViewController.selectItemIndex = index;
            mainViewController.dismissBlock = ^{
                weakMoveBth.hidden = NO;
            };
            [self removeAllSubView];
            [JBW_KEY_WINDOW.rootViewController presentViewController:mainViewController animated:NO completion:nil];
        };
        
        [JBWAlertView showRealNameAuthentication:nil suerBlcok:^{
            weakMoveBth.hidden = YES;
            JBWPOPRealNameAuthenticationViewController *pealNameAuthenticationVC = [JBWPOPRealNameAuthenticationViewController new] ;
            pealNameAuthenticationVC.dismissBlock = ^{
                weakMoveBth.hidden = NO;
            };
            [self removeAllSubView];
            [JBW_KEY_WINDOW.rootViewController presentViewController:pealNameAuthenticationVC animated:NO completion:nil];
        }];
    }];
    
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}


@end
